<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      inline
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="编号/摘要" prop="searchValue">
        <el-input
          v-model="queryParams.searchValue"
          placeholder="输入编号/订单摘要"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单类型" prop="shoppingCarEnums">
        <el-select
          v-model="queryParams.shoppingCarEnums"
          placeholder="选择订单类型"
          @change="handleQuery"
        >
          <el-option
            v-for="item in orderTypeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row class="mb8" :gutter="10">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="dataList" border>
      <el-table-column label="编号" align="center" prop="shoppingCarId" width="120" />
      <el-table-column label="订单类型" align="center" width="120">
        <template slot-scope="scope">
          {{ getEnumsLabel(scope.row.shoppingCarEnums) }}
        </template>
      </el-table-column>
      <el-table-column label="期限数量" align="center" prop="periodNum" width="120" />
      <el-table-column label="购买方式" align="center" width="120">
        <template slot-scope="scope">
          {{ scope.row.periodType | ctChargeType }}
        </template>
      </el-table-column>
      <el-table-column label="自动续费" align="center" prop="autoRenew" width="120">
        <template slot-scope="scope">
          {{ scope.row.autoRenew ? "是" : "否" }}
        </template>
      </el-table-column>
      <el-table-column label="订单摘要" align="center" prop="orderAbstract" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
      <el-table-column
        label="原始价格(元)"
        align="center"
        prop="originalPrice"
        width="140"
      />
      <el-table-column
        label="修改价格(元)"
        align="center"
        prop="updatePrice"
        width="140"
      />
      <el-table-column
        label="优惠的金额(元)"
        align="center"
        prop="discountPrice"
        width="140"
      />
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:car:edit']"
          >
            改价
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.params.pageNum"
      :limit.sync="queryParams.params.pageSize"
      @pagination="getList"
    />
    <el-dialog
      title="改价"
      :visible.sync="editShow"
      :close-on-click-modal="false"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="原始价格" prop="originalPrice">
          {{ form.originalPrice || 0 }}元
        </el-form-item>
        <el-form-item label="修改价格" prop="updatePrice">
          <el-input v-model="form.updatePrice" placeholder="请输入修改价格">
            <span slot="suffix">元</span>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="editShow = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCar, updateCar } from "@/api/system/car";

export default {
  name: "car",
  data() {
    return {
      loading: false, // 遮罩层
      showSearch: true, // 显示搜索条件
      total: 0, // 总条数
      dataList: [], // 购物车表格数据
      //订单类型列表
      orderTypeList: [
        { value: "CLOUD_SERVER", label: "云服务器" },
        { value: "CLOUD_MARKET", label: "云市场" },
        { value: "GOV_CLOUD", label: "政务云" },
        { value: "CLOUD_SERVER_RENEW", label: "云服务器续费" },
        { value: "CLOUD_SERVER_CHANGE", label: "云服务器变更" },
      ],
      // 查询参数
      queryParams: {
        searchValue: null,
        shoppingCarEnums: null,
        params: {
          pageNum: 1,
          pageSize: 10,
        },
      },
      editShow: false, // 改价弹窗
      form: {},
      rules: {
        updatePrice: [
          { required: true, message: "价格不能为空", trigger: "blur" },
          { pattern: /^(0\.\d+|[1-9]\d*(\.\d+)?)$/, message: "价格不能小于等于0" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询购物车列表 */
    getList() {
      this.loading = true;
      listCar(this.queryParams)
        .then((res) => {
          this.dataList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 表单重置
    reset() {
      this.form = {
        updatePrice: null,
        originalPrice: null,
        discountPrice: null,
        shoppingCarEnums: null,
        orderAbstract: null,
        discount: null,
        autoRenew: null,
        periodNum: null,
        periodType: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.params.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 改价
    handleUpdate(row) {
      this.form = Object.assign({}, row);
      this.editShow = true;
    },
    // 获取类型中文名称
    getEnumsLabel(val) {
      let row = this.orderTypeList.find((i) => i.value == val);
      return row ? row.label : "";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          updateCar(this.form).then((res) => {
            this.$modal.msgSuccess("修改成功");
            this.editShow = false;
            this.getList();
          });
        }
      });
    },
  },
};
</script>
